<template>
  <page-layout class="user">
    <div class="search-wrapper">
      <div>
        <a-input placeholder="请输入用户名称" class="search-input">
          <a-icon
            slot="prefix"
            type="search"
            style="font-size: 28px; color: #999"
          />
        </a-input>
        <a-button type="primary" class="search-button">搜索</a-button>
      </div>
      <a-button
        style="width: 120px; height: 54px"
        type="primary"
        @click="
          $router.push({
            path: '/setting/userAdd',
            query: { random: random }
          })
        "
        >新增</a-button
      >
    </div>

    <!-- <a-card> -->
    <div class="tabel-wrapper">
      <!-- <div class="operator">
          <a-button @click="addNew" type="primary">新建</a-button>
          <a-button>批量操作</a-button>
          <a-dropdown>
            <a-menu @click="handleMenuClick" slot="overlay">
              <a-menu-item key="delete">删除</a-menu-item>
              <a-menu-item key="audit">审批</a-menu-item>
            </a-menu>
            <a-button> 更多操作 <a-icon type="down" /> </a-button>
          </a-dropdown>
        </div> -->
      <standard-table
        :columns="columns"
        :dataSource="dataSource"
        @change="onChange"
        :pagination="pagination"
        size="large"
        :loading="loading"
      >
        <div slot="roles" slot-scope="{ text }" style="max-width: 450px">
          <!-- {{ text }} -->
          <template v-if="text && text.length > 0">
            <template v-for="(item, index) in text">
              <a-tag
                style="margin-top: 5px"
                color="blue"
                v-if="item"
                :key="index"
              >
                {{ item || 1 }}
              </a-tag>
            </template>
          </template>
          <a-tag v-else color="#c4c4c4"> 暂无绑定角色 </a-tag>
        </div>
        <!-- eslint-disable-next-line -->
        <div slot="status" slot-scope="{ text, record }">
          <!-- {{ text.status }}status -->
          <!-- <a-switch
            :checked="
              Boolean(
                dataSource[record.key] || dataSource[record.key].status || 0
              )
            "
            @change="(val) => handelchandel(record.key, val)"
          /> -->
           <a-popover placement="bottomRight">
                  <template slot="content">
                    <h5>点击变更职工状态</h5>
                  </template>
                  <a-tag
            @click="changeUserStatus(record.key)"
            :style="`
              width: 70px;
              height: 30px;
              text-align: center;
              line-height: 30px;
            `"
            :color="`${text.status === 1 ? '#00CA13' : '#FF2525'}`"
          >
            {{ text.status === 1 ? "在职" : "离职" }}
          </a-tag>
                </a-popover>
         
        </div>
        <div slot="action" slot-scope="{ text, record }">
          <!-- {{ text }} -->
          <a :href="`#/setting/userAdd?random=${random}&&id=${record.key}`"
            >编辑</a
          >
          <a-divider type="vertical" style="background: #0097e0" />
          <a-popconfirm
            title="确定需要删除此账号吗?"
            ok-text="是的"
            cancel-text="取消"
            @confirm="() => confirm(record.key)"
            @cancel="() => cancel(record.key)"
            ><a>删除</a>
          </a-popconfirm>
        </div>
        <template slot="statusTitle">
          <a-icon @click.native="onStatusTitleClick" type="info-circle" />
        </template>
      </standard-table>
    </div>
    <!-- </a-card> -->
  </page-layout>
</template>

<script>
import PageLayout from "@/layouts/PageLayout";
import StandardTable from "@/components/table/StandardTable";
import { getUsers, deleteItem ,changeUserStatus} from "@/services/user";
// import { mapMutations } from "vuex";
const columns = [
  {
    title: "ID",
    dataIndex: "key"
  },
  {
    title: "部门",
    dataIndex: "department_name"
  },
  {
    title: "用户名",
    dataIndex: "username"
  },
  {
    title: "昵称",
    dataIndex: "name"
  },
  {
    title: "邮箱",
    dataIndex: "email"
  },
  
  {
    title: "角色",
    dataIndex: "roles",
    scopedSlots: { customRender: "roles" }
    // width: "200px"
  },
  {
    title: "在职状态",
    // dataIndex: "status",
    scopedSlots: { customRender: "status" }
  },
  {
    title: "更多",
    scopedSlots: { customRender: "action" },
    align: "center"
  }
];

export default {
  name: "demand",
  components: { PageLayout, StandardTable },
  data() {
    return {
      searchText: "",
      random: "",
      loading: true,
      // desc: "需求管理中心",
      advanced: true,
      columns: columns,
      dataSource: [],
      selectedRows: [],
      pagination: {
        size: "small",
        showSizeChanger: true,
        showQuickJumper: true,
        poposition: "bottom",
        pageSize: 5,
        current: 1,
        total: 0
        // hideOnSinglePage: true
      }
    };
  },
  authorize: {
    deleteRecord: "delete"
  },
  created() {
    const { pageSize, current } = this.pagination;
    this.random = Math.random();
    this.loadTableData({ limit: pageSize, page: current });
  },
  activated() {
    // 初始数据
    this.random = Math.random();
    const { pageSize, current } = this.pagination;
    this.loadTableData({ limit: pageSize, page: current });
  },
  methods: {
    changeUserStatus(user_id){

       changeUserStatus(user_id).then((res) => {
        // console.log(res, "delete");
        // 成功
        if (res.data.code === 20000) {
          // 提示信息
          this.$message.success({
            content: res.data.message || "修改成功~"
          });
          // 修改成功后重新获取数据
          const { pageSize, current } = this.pagination;
          this.loadTableData({ limit: pageSize, page: current });
        }
      });
    } ,
    // 请求table 的数据
    // obj 请求的字段
    loadTableData(obj) {
      this.loading = true;
      getUsers(obj).then((res) => {
        // console.log(res.data.data.data, 222222);
        // 关闭loading
        this.loading = false;
        if (res.data.code === 20000) {
          const { current_page, total, per_page } = res.data.data;
          Object.assign(this.pagination, {
            total: parseInt(total),
            current: parseInt(current_page),
            pageSize: parseInt(per_page)
          });
          this.dataSource = res.data.data.data.map((item) => {
            // console.log(item);
            const {
              id,
              department_name,
              username,
              name,
              email,
              roles,
              status
            } = item;
            let roleArr = [];
            for (let key in roles) {
              // console.log(key);
              roleArr[key] = roles[key];
            }
            console.log(roleArr);
            return {
              key: id,
              username: username || "未知",
              department_name: department_name || "未知",
              name: name || "未知",
              email: email || "未知",
              roles: roleArr || [],
              status: status || 0
            };
          });
        }
      });
    },
    handelsearch() {
      this.loadTableData({
        limit: this.pagination.pageSize,
        title: this.searchText
      });
      this.searchText = "";
    },
    deleteRecord(key) {
      this.dataSource = this.dataSource.filter((item) => item.key !== key);
      this.selectedRows = this.selectedRows.filter((item) => item.key !== key);
    },
    toggleAdvanced() {
      this.advanced = !this.advanced;
    },
    onClear() {
      this.$message.info("您清空了勾选的所有行");
    },
    onStatusTitleClick() {
      this.$message.info("你点击了状态栏表头");
    },
    onChange(e) {
      const { current, pageSize } = e;
      this.loadTableData({ limit: pageSize, page: current });
    },
    handleMenuClick(e) {
      if (e.key === "delete") {
        this.remove();
      }
    },
    cancel(key) {
      this.$message.info(`点击了取消,行数为${key + 1}行`);
    },
    confirm(key) {
      // 发起删除的请求
      deleteItem(key).then((res) => {
        // console.log(res, "delete");
        // 成功
        if (res.data.code === 20000) {
          // 提示信息
          this.$message.success({
            content: res.data.message || "删除成功~"
          });
          // 删除成功后重新获取数据
          const { pageSize, current } = this.pagination;
          this.loadTableData({ limit: pageSize, page: current });
        }
      });
      // this.$message.info(`点击了确定,行数为${key + 1}行`);
      // this.deleteRecord(key);
    },
    handelchandel(key, val) {
      console.log(val, "chacked");
      console.log(key, "key");
      this.dataSource[key].status = !this.dataSource[key].status;
    }
  }
};
</script>

<style lang="less">
.search-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;

  > div {
    .search-input input {
      padding-left: 45px !important;
    }
    display: flex;
    width: 60%;
    .ant-input-affix-wrapper {
      display: inline-block;
      width: calc(100% - 150px);
      height: 54px !important;
      input {
        height: 100% !important;
      }
      input::-webkit-input-placeholder {
        color: #9999;
        font-size: 16px;
      }

      input::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #9999;
        font-size: 16px;
      }

      input:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: #9999;
        font-size: 16px;
      }

      input:-ms-input-placeholder {
        /* Internet Explorer 10-11 */
        color: #9999;
        font-size: 16px;
      }
    }
    .search-button {
      margin-left: 30px;
      width: 120px;
      height: 54px;
    }
  }
}
.user {
  .tabel-wrapper {
    border-radius: 5px;
    // min-height: 450px;
    background-color: #fff;
    color: #1f1f1f;
    .ant-pagination {
      margin-top: 60px !important;
    }
    tr {
      height: 66px !important;
      // padding-left: 10px;
    }
    .ant-table-thead {
      padding-left: 20px;
      background: #effaff;
      & > tr > th {
        background: #effaff !important;
      }
    }
    .ant-table-tbody {
      padding-left: 20px;
    }
    .ant-table-pagination.ant-pagination {
      float: unset !important;
      text-align: center;
    }
  }
}
</style>
